//
//  YWMeterHomeViewController.m
//  maintain
//
//  Created by 烟雨云渊 on 2017/4/25.
//  Copyright © 2017年 ewin. All rights reserved.
//

#import "YWMeterHomeViewController.h"
#import "YWMeterReadingRecordViewController.h"
#import "YWDoMeterReadingTableViewController.h"
#import "YWSelectMeterReadingEquipmentTableViewController.h"
#import "YWScanEquipmentNotMatchVC.h"
#import "YWMeterWaitReadingTableViewController.h"
#import "YWMeterHaveReadTableViewController.h"

#import "Masonry.h"
#import "UIColor+CustomColors.h"
#import "UIImage+Extension.h"
#import "YWCommonTool.h"
#import "YWConst.h"
#import "YWMacro.h"
#import "YWNoQrcodeRights.h"
#import "YWQrcodeManager.h"
#import "YWMeterReadingManager.h"
#import "YWLoginTool.h"
#import "UIView+Extension.h"
#import "YWThisMonthReadingMeterCount.h"
#import "YWEquipmentType.h"
#import "YWEquipmentManager.h"

@interface YWMeterHomeViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) UIView * meterHomeView;
//水表
@property (nonatomic, strong) UILabel * waterTodayCountLb;
@property (nonatomic, strong) UILabel * waterMonthCountLb;
@property (nonatomic, strong) UIButton * waterMonthLeftBtn;

//电表
@property (nonatomic, strong) UILabel * eleTodayCountLb;
@property (nonatomic, strong) UILabel * eleMonthCountLb;
@property (nonatomic, strong) UIButton * eleMonthLeftBtn;

//气表
@property (nonatomic, strong) UILabel * gasTodayCountLb;
@property (nonatomic, strong) UILabel * gasMonthCountLb;
@property (nonatomic, strong) UIButton * gasMonthLeftBtn;

@property (nonatomic, strong) UITextField * meterNumberTf;
@property (nonatomic, strong) UIButton * meterNumberReadBtn;
@property (nonatomic, strong) UILabel * noteLabel;

@property (nonatomic, strong) UIButton *noScanButton;
@property (nonatomic, strong) UIButton *scanButton;
@end

@implementation YWMeterHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"抄表";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = [UIColor whiteColor];
    [self layoutView];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"抄表记录" style:UIBarButtonItemStyleDone target:self action:@selector(readingMeterRecord)];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    YWWeakSelf(weakSelf);
    [[YWMeterReadingManager shareManager] requestThisMonthReadingMeterCount:^(BOOL success) {
        if (success) {
            [weakSelf showStatisticsNumber];
        }
    }];
}

- (void)showStatisticsNumber
{
    [self showWaterMeterStatisticsNumber];
    [self showElectricMeterStatisticsNumber];
    [self showGasMeterStatisticsNumber];
}

- (void)showWaterMeterStatisticsNumber
{
    //水表
    NSInteger wEquipmentTypeId = [[YWMeterReadingManager shareManager]waterMeterEquipmentTypeId];
    NSArray* waterData = [[YWMeterReadingManager shareManager] allThisMonthReadMeterCount:wEquipmentTypeId];
    NSInteger waterMeterTotalReadCount = 0;
    NSInteger waterMeterReadedCount = 0;
    for (YWThisMonthReadingMeterCount* thisMonthReadingMeterCount in waterData) {
        waterMeterTotalReadCount += thisMonthReadingMeterCount.total;
        waterMeterReadedCount += thisMonthReadingMeterCount.recordedAmount;
    }
    self.waterMonthCountLb.text = [NSString stringWithFormat:@"%ld", (long)waterMeterReadedCount];
    
    NSString * waterOverStr = [NSString stringWithFormat:@"%ld", (long)(waterMeterTotalReadCount - waterMeterReadedCount)];
    [self.waterMonthLeftBtn setTitle:waterOverStr forState:UIControlStateNormal];
    self.waterTodayCountLb.text = [NSString stringWithFormat:@"%ld", (long)[[YWMeterReadingManager shareManager] toDayReadMeterCountWithEquipmentTypeId:wEquipmentTypeId]];
}

- (void)showElectricMeterStatisticsNumber
{
    //电表
    NSInteger eEquipmentTypeId = [[YWMeterReadingManager shareManager]electricMeterEquipmentTypeId];
    NSArray* electricityData = [[YWMeterReadingManager shareManager] allThisMonthReadMeterCount:eEquipmentTypeId];
    NSInteger electricMeterTotalReadCount = 0;
    NSInteger electricMeterReadedCount = 0;
    for (YWThisMonthReadingMeterCount* thisMonthReadingMeterCount in electricityData) {
        electricMeterTotalReadCount += thisMonthReadingMeterCount.total;
        electricMeterReadedCount += thisMonthReadingMeterCount.recordedAmount;
    }
    self.eleMonthCountLb.text = [NSString stringWithFormat:@"%ld", (long)electricMeterReadedCount];
    NSString * eleOverStr = [NSString stringWithFormat:@"%ld", (long)(electricMeterTotalReadCount - electricMeterReadedCount)];
    [self.eleMonthLeftBtn setTitle:eleOverStr forState:UIControlStateNormal];
    self.eleTodayCountLb.text = [NSString stringWithFormat:@"%ld", (long)[[YWMeterReadingManager shareManager] toDayReadMeterCountWithEquipmentTypeId:eEquipmentTypeId]];
}

- (void)showGasMeterStatisticsNumber
{
    //煤气
    NSInteger gEquipmentTypeId = [[YWMeterReadingManager shareManager]gasMeterEquipmentTypeId];
    NSArray* gasData = [[YWMeterReadingManager shareManager] allThisMonthReadMeterCount:gEquipmentTypeId];
    NSInteger gasMeterTotalReadCount = 0;
    NSInteger gasMeterReadedCount = 0;
    for (YWThisMonthReadingMeterCount* thisMonthReadingMeterCount in gasData) {
        gasMeterTotalReadCount += thisMonthReadingMeterCount.total;
        gasMeterReadedCount += thisMonthReadingMeterCount.recordedAmount;
    }
    self.gasMonthCountLb.text = [NSString stringWithFormat:@"%ld", (long)gasMeterReadedCount];
    NSString * gasOverStr = [NSString stringWithFormat:@"%ld", (long)(gasMeterTotalReadCount - gasMeterReadedCount)];
    [self.gasMonthLeftBtn setTitle:gasOverStr forState:UIControlStateNormal];
    self.gasTodayCountLb.text = [NSString stringWithFormat:@"%ld", (long)[[YWMeterReadingManager shareManager] toDayReadMeterCountWithEquipmentTypeId:gEquipmentTypeId]];
}

- (void)touchesBegan:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event
{
    [self.view endEditing:YES];
}

- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string

{
    NSString* text = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if (text.length > 0) {
        self.meterNumberReadBtn.enabled = YES;
        [self.meterNumberReadBtn setTitleColor:[UIColor customBlueColor] forState:UIControlStateNormal];
    }
    else {
        self.meterNumberReadBtn.enabled = NO;
        [self.meterNumberReadBtn setTitleColor:[UIColor customTextGrayColor] forState:UIControlStateNormal];
    }
    return YES;
}


#pragma mark - 懒加载
- (void)layoutView{
    self.meterNumberTf.frame = CGRectMake(kMarginW*3,CGRectGetMaxY(self.meterHomeView.frame)+kMarginW*2, SCREEN_WIDTH/2, KbottomViewH);
    [self.view addSubview:self.meterNumberTf];
    self.meterNumberReadBtn.frame = CGRectMake(CGRectGetMaxX(self.meterNumberTf.frame)+2*kMarginW, CGRectGetMaxY(self.meterHomeView.frame)+kMarginW*2, 80, KbottomViewH);
    [self.view addSubview:self.meterNumberReadBtn];
    
    self.noteLabel.frame = CGRectMake(2*kMarginW, CGRectGetMaxY(self.meterNumberTf.frame)+2*kMarginW, SCREEN_WIDTH-4*kMarginW, 15);
    [self.view addSubview:self.noteLabel];
    
    [self.view addSubview:self.noScanButton];
    [self.view addSubview:self.scanButton];
}

- (UIView*)meterHomeView{
    if (!_meterHomeView) {
        UIView *meterHomeView = [[UIView alloc] init];
        meterHomeView.frame = CGRectMake(kMarginW, kGapH, SCREEN_WIDTH-2*kMarginW, KbottomViewH*4);
        meterHomeView.backgroundColor = [UIColor whiteColor];
        meterHomeView.layer.borderWidth = 0.5;
        meterHomeView.layer.borderColor = [UIColor customTextGrayBlackColor].CGColor;
        
        CGFloat countWidth = 104;
        CGFloat meterNameLbWidth = (SCREEN_WIDTH-2*kMarginW-countWidth)/3;
        
        NSArray * meterItems = @[@"水表",@"电表",@"气表"];
        NSArray * countItems = @[@"",@"今日抄表数",@"本月抄表数",@"本月剩余数"];
        for (int i = 0; i<meterItems.count; i++) {
            UILabel * meterNameLb = [[UILabel alloc] init];
            meterNameLb.frame = CGRectMake(countWidth+meterNameLbWidth*i, 0, meterNameLbWidth, KbottomViewH);
            meterNameLb.textAlignment = NSTextAlignmentCenter;
            meterNameLb.layer.borderWidth = 0.5;
            meterNameLb.layer.borderColor = [UIColor customTextGrayBlackColor].CGColor;
            meterNameLb.text = meterItems[i];
            meterNameLb.font = kContentFont;
            [meterHomeView addSubview:meterNameLb];
        }
        for (int j = 0; j<countItems.count; j++) {
            UILabel * meterCountLb = [[UILabel alloc] init];
            meterCountLb.frame = CGRectMake(0, KbottomViewH*j, countWidth, KbottomViewH);
            meterCountLb.textAlignment = NSTextAlignmentCenter;
            meterCountLb.layer.borderWidth = 0.5;
            meterCountLb.layer.borderColor = [UIColor customTextGrayBlackColor].CGColor;
            meterCountLb.text = countItems[j];
            meterCountLb.font = kContentFont;
            [meterHomeView addSubview:meterCountLb];
        }
        
        self.waterTodayCountLb.frame = CGRectMake(countWidth, KbottomViewH, meterNameLbWidth, KbottomViewH);
        self.waterMonthCountLb.frame = CGRectMake(countWidth, KbottomViewH*2,meterNameLbWidth, KbottomViewH);
        self.waterMonthLeftBtn.frame = CGRectMake(countWidth, KbottomViewH*3,meterNameLbWidth, KbottomViewH);
        
        self.eleTodayCountLb.frame = CGRectMake(countWidth+meterNameLbWidth, KbottomViewH, meterNameLbWidth, KbottomViewH);
        self.eleMonthCountLb.frame = CGRectMake(countWidth+meterNameLbWidth, KbottomViewH*2, meterNameLbWidth, KbottomViewH);
        self.eleMonthLeftBtn.frame = CGRectMake(countWidth+meterNameLbWidth, KbottomViewH*3, meterNameLbWidth, KbottomViewH);
        
        self.gasTodayCountLb.frame = CGRectMake(countWidth+meterNameLbWidth*2, KbottomViewH, meterNameLbWidth, KbottomViewH);
        self.gasMonthCountLb.frame = CGRectMake(countWidth+meterNameLbWidth*2, KbottomViewH*2, meterNameLbWidth, KbottomViewH);
        self.gasMonthLeftBtn.frame = CGRectMake(countWidth+meterNameLbWidth*2, KbottomViewH*3, meterNameLbWidth, KbottomViewH);
        
        [meterHomeView addSubview:self.waterTodayCountLb];
        [meterHomeView addSubview:self.waterMonthCountLb];
        [meterHomeView addSubview:self.waterMonthLeftBtn];
        
        [meterHomeView addSubview:self.eleTodayCountLb];
        [meterHomeView addSubview:self.eleMonthCountLb];
        [meterHomeView addSubview:self.eleMonthLeftBtn];
        
        [meterHomeView addSubview:self.gasTodayCountLb];
        [meterHomeView addSubview:self.gasMonthCountLb];
        [meterHomeView addSubview:self.gasMonthLeftBtn];
        _meterHomeView = meterHomeView;
        [self.view addSubview:_meterHomeView];
    }
    return _meterHomeView;
}

- (UILabel*)waterTodayCountLb{
    if (!_waterTodayCountLb) {
        UILabel * label = [[UILabel alloc] init];
        label.layer.borderColor = [UIColor customTextGrayBlackColor].CGColor;
        label.layer.borderWidth = 0.5;
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"0";
        label.font = kContentFont;
        _waterTodayCountLb = label;
        [self.meterHomeView addSubview:_waterTodayCountLb];
    }
    return _waterTodayCountLb;
}

- (UILabel*)waterMonthCountLb{
    if (!_waterMonthCountLb) {
        UILabel * label = [[UILabel alloc] init];
        label.layer.borderColor = [UIColor customTextGrayBlackColor].CGColor;
        label.layer.borderWidth = 0.5;
        label.textAlignment = NSTextAlignmentCenter;
        label.userInteractionEnabled = YES;
        label.textColor = [UIColor customBlueColor];
        label.text = @"0";
        label.font = kContentFont;
        [label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleWaterMonthCountLbTapEvent:)]];
        _waterMonthCountLb = label;
        [self.meterHomeView addSubview:_waterMonthCountLb];
    }
    return _waterMonthCountLb;
}

- (UIButton *)waterMonthLeftBtn{
    if (!_waterMonthLeftBtn) {
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"0" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor customBlueColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(waterMonthLeftBtnEvent:) forControlEvents:UIControlEventTouchUpInside];
        button.layer.borderColor = [UIColor customTextGrayBlackColor].CGColor;
        button.layer.borderWidth = 0.5;
        _waterMonthLeftBtn = button;
        [self.meterHomeView addSubview:_waterMonthLeftBtn];
    }
    return _waterMonthLeftBtn;
}

- (UILabel*)eleTodayCountLb{
    if (!_eleTodayCountLb) {
        UILabel * label = [[UILabel alloc] init];
        label.layer.borderColor = [UIColor customTextGrayBlackColor].CGColor;
        label.layer.borderWidth = 0.5;
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"0";
        label.font = kContentFont;
        _eleTodayCountLb = label;
        [self.meterHomeView addSubview:_eleTodayCountLb];
    }
    return _eleTodayCountLb;
}
- (UILabel*)eleMonthCountLb{
    if (!_eleMonthCountLb) {
        UILabel * label = [[UILabel alloc] init];
        label.layer.borderColor = [UIColor customTextGrayBlackColor].CGColor;
        label.layer.borderWidth = 0.5;
        label.textAlignment = NSTextAlignmentCenter;
        label.userInteractionEnabled = YES;
        label.textColor = [UIColor customBlueColor];
        label.text = @"0";
        label.font = kContentFont;
        [label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleEleMonthCountLbTapEvent:)]];
        _eleMonthCountLb = label;
        [self.meterHomeView addSubview:_eleMonthCountLb];
    }
    return _eleMonthCountLb;
}

- (UIButton *)eleMonthLeftBtn{
    if (!_eleMonthLeftBtn) {
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"0" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor customBlueColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(eleMonthLeftBtnEvent:) forControlEvents:UIControlEventTouchUpInside];
        button.layer.borderColor = [UIColor customTextGrayBlackColor].CGColor;
        button.layer.borderWidth = 0.5;
        _eleMonthLeftBtn = button;
        [self.meterHomeView addSubview:_eleMonthLeftBtn];
    }
    return _eleMonthLeftBtn;
}

- (UILabel*)gasTodayCountLb{
    if (!_gasTodayCountLb) {
        UILabel * label = [[UILabel alloc] init];
        label.layer.borderColor = [UIColor customTextGrayBlackColor].CGColor;
        label.layer.borderWidth = 0.5;
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"0";
        label.font = kContentFont;
        _gasTodayCountLb = label;
        [self.meterHomeView addSubview:_gasTodayCountLb];
    }
    return _gasTodayCountLb;
}
- (UILabel*)gasMonthCountLb{
    if (!_gasMonthCountLb) {
        UILabel * label = [[UILabel alloc] init];
        label.layer.borderColor = [UIColor customTextGrayBlackColor].CGColor;
        label.layer.borderWidth = 0.5;
        label.textAlignment = NSTextAlignmentCenter;
        label.userInteractionEnabled = YES;
        label.textColor = [UIColor customBlueColor];
        label.text = @"0";
        label.font = kContentFont;
        [label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGasMonthCountLbTapEvent:)]];
        _gasMonthCountLb = label;
        [self.meterHomeView addSubview:_gasMonthCountLb];
    }
    return _gasMonthCountLb;
}

- (UIButton *)gasMonthLeftBtn{
    if (!_gasMonthLeftBtn) {
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"0" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor customBlueColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(gasMonthLeftBtnEvent:) forControlEvents:UIControlEventTouchUpInside];
        button.layer.borderColor = [UIColor customTextGrayBlackColor].CGColor;
        button.layer.borderWidth = 0.5;
        _gasMonthLeftBtn = button;
        [self.meterHomeView addSubview:_gasMonthLeftBtn];
    }
    return _gasMonthLeftBtn;
}

- (UITextField*)meterNumberTf{
    if (!_meterNumberTf) {
        UITextField * meterNumberTf = [[UITextField alloc] init];
        meterNumberTf.placeholder = @"输入表号";
        meterNumberTf.font = kContentFont;
        meterNumberTf.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 1)];
        meterNumberTf.leftViewMode = UITextFieldViewModeAlways;
        meterNumberTf.delegate = self;
        meterNumberTf.layer.masksToBounds = YES;
        meterNumberTf.layer.cornerRadius = 5.0;
        meterNumberTf.layer.borderWidth = 0.5;
        meterNumberTf.layer.borderColor = [UIColor customLineColor].CGColor;
        _meterNumberTf = meterNumberTf;
    }
    return _meterNumberTf;
}

- (UIButton*)meterNumberReadBtn{
    if (!_meterNumberReadBtn) {
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.enabled = NO;
        [button setTitle:@"表号抄表" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor customTextGrayColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(meterNumberReadBtnEvent:) forControlEvents:UIControlEventTouchUpInside];
        _meterNumberReadBtn = button;
    }
    return _meterNumberReadBtn;
}

- (UILabel*)noteLabel{
    if (!_noteLabel) {
        UILabel * label = [[UILabel alloc] init];
        label.text = @"如果设备二维码损坏，请输入表号进行抄表";
        label.textColor = [UIColor customTextGrayBlackColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:12];
        _noteLabel = label;
    }
    return _noteLabel;
}


- (UIButton *)scanButton{
    if (!_scanButton) {
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"扫码" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        button.backgroundColor = [UIColor customBlueColor];
        [button addTarget:self action:@selector(clickScanBtnEvent:) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake(SCREEN_WIDTH/4, SCREEN_HEIGHT-64-KbottomViewH, SCREEN_WIDTH/4*3, KbottomViewH);
        _scanButton = button;
    }
    return _scanButton;
}

- (UIButton *)noScanButton{
    if (!_noScanButton) {
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"无码" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        button.backgroundColor = [UIColor customYellowColor];
        [button addTarget:self action:@selector(handleNoScanBtnEvent:) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake(0, SCREEN_HEIGHT-64-KbottomViewH, SCREEN_WIDTH/4, KbottomViewH);
        _noScanButton = button;
    }
    return _noScanButton;
}


#pragma mark - 事件的处理
- (void)readingMeterRecord
{
    YWMeterReadingRecordViewController* vc = [[YWMeterReadingRecordViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)clickScanBtnEvent:(id)sender{
    [[YWQrcodeManager shareManager]clickScanButtonWithType:YWQrcodeScanTypeMeterReading ViewController:self completionBlock:^(NSString *qrcodeId, QRCodeReaderViewController *qrCodeReaderViewController) {
        //上传扫码日志
        [[YWQrcodeManager shareManager] postQrcodeLogWithQrcodeId:qrcodeId qrcodeLogType:YWQrcodeLogTypeMeterReading];
        dispatch_async(dispatch_get_main_queue(), ^{
            //            [qrCodeReaderViewController dismissViewControllerAnimated:NO completion:^{
            [self qrcodeOrNoScanWithEquipment:[[YWMeterReadingManager shareManager] meterQRCoder:qrcodeId] qrCodeReaderVC:qrCodeReaderViewController scan:YES];
            //            }];
        });
    }];
}

- (void)handleNoScanBtnEvent:(id)sender{
    NSInteger value = [[[NSUserDefaults standardUserDefaults] objectForKey:KYWNoQrcodeTypeMeterReadingKey] integerValue];
    if (value==1) {
        UIAlertController* alertController= [UIAlertController alertControllerWithTitle:@"无码工作已被管理员禁止" message:nil preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
        [self presentViewController:alertController animated:YES completion:nil];
    }else{
        YWSelectMeterReadingEquipmentTableViewController *selectMeterEquiVC = [[YWSelectMeterReadingEquipmentTableViewController alloc] init];
        __weak __typeof(&*selectMeterEquiVC) weakSelectMeterEquiVC = selectMeterEquiVC;
        selectMeterEquiVC.meterReadingBlock = ^(YWEquipment *equipment) {
            __strong __typeof(&*weakSelectMeterEquiVC) strongSelectMeterEquiVC = weakSelectMeterEquiVC;
            [self qrcodeOrNoScanWithEquipment:equipment qrCodeReaderVC:strongSelectMeterEquiVC scan:NO];
        };
        [self.navigationController pushViewController:selectMeterEquiVC animated:YES];
    }
}

- (void)qrcodeOrNoScanWithEquipment:(YWEquipment*)equipment qrCodeReaderVC:(UIViewController*)qrCodeReaderVC scan:(BOOL)isScan{
    if (equipment) {
        YWDoMeterReadingTableViewController* meterReadingVC = [[YWDoMeterReadingTableViewController alloc] init];
        meterReadingVC.equipment = equipment;
        meterReadingVC.enterFromButtonOrNoQrcode = !isScan;
        [qrCodeReaderVC.navigationController pushViewController:meterReadingVC animated:YES];
    }
    else {
        YWScanEquipmentNotMatchVC* senmVC = [[YWScanEquipmentNotMatchVC alloc] init];
        senmVC.showContent = NSLocalizedString(kTextMeterReadingNoMeter, nil);
        [qrCodeReaderVC.navigationController pushViewController:senmVC animated:YES];
    }
}

- (void)meterNumberReadBtnEvent:(id)sender{
    [self.view endEditing:YES];
    YWEquipment* equipment = [[YWMeterReadingManager shareManager] meterNumber:self.meterNumberTf.text];
    if (equipment) {
        YWDoMeterReadingTableViewController* meterReadingVC = [[YWDoMeterReadingTableViewController alloc] init];
        meterReadingVC.equipment = equipment;
        meterReadingVC.enterFromButtonOrNoQrcode = YES;
        [self.navigationController pushViewController:meterReadingVC animated:YES];
    }
    else {
        [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextMeterNumberReadingNoMeter, nil)];
    }
}

- (void)handleWaterMonthCountLbTapEvent:(id)sender{
    YWEquipmentType * eleEquType = [[YWEquipmentType alloc] init];
    NSInteger equipmentTypeId = [[YWMeterReadingManager shareManager] waterMeterEquipmentTypeId];
    NSString *equipmentTypeName = [[YWEquipmentManager shareManager] equipmentTypeNameWithTypeId:equipmentTypeId];
    eleEquType.equipmentTypeId = equipmentTypeId;
    eleEquType.equipmentTypeName = equipmentTypeName;
    YWMeterHaveReadTableViewController * waitReadingVC = [[YWMeterHaveReadTableViewController alloc] init];
    waitReadingVC.equipmentType = eleEquType;
    [self.navigationController pushViewController:waitReadingVC animated:YES];
}

- (void)handleEleMonthCountLbTapEvent:(id)sender{
    YWEquipmentType * eleEquType = [[YWEquipmentType alloc] init];
    NSInteger equipmentTypeId = [[YWMeterReadingManager shareManager] electricMeterEquipmentTypeId];
    NSString *equipmentTypeName = [[YWEquipmentManager shareManager] equipmentTypeNameWithTypeId:equipmentTypeId];
    eleEquType.equipmentTypeId = equipmentTypeId;
    eleEquType.equipmentTypeName = equipmentTypeName;
    YWMeterHaveReadTableViewController * waitReadingVC = [[YWMeterHaveReadTableViewController alloc] init];
    waitReadingVC.equipmentType = eleEquType;
    [self.navigationController pushViewController:waitReadingVC animated:YES];
}

- (void)handleGasMonthCountLbTapEvent:(id)sender{
    YWEquipmentType * eleEquType = [[YWEquipmentType alloc] init];
    NSInteger equipmentTypeId = [[YWMeterReadingManager shareManager] gasMeterEquipmentTypeId];
    NSString *equipmentTypeName = [[YWEquipmentManager shareManager] equipmentTypeNameWithTypeId:equipmentTypeId];
    eleEquType.equipmentTypeId = equipmentTypeId;
    eleEquType.equipmentTypeName = equipmentTypeName;
    YWMeterHaveReadTableViewController * waitReadingVC = [[YWMeterHaveReadTableViewController alloc] init];
    waitReadingVC.equipmentType = eleEquType;
    [self.navigationController pushViewController:waitReadingVC animated:YES];
}

- (void)waterMonthLeftBtnEvent:(UIButton*)sender{
    YWEquipmentType * eleEquType = [[YWEquipmentType alloc] init];
    NSInteger equipmentTypeId = [[YWMeterReadingManager shareManager] waterMeterEquipmentTypeId];
    NSString *equipmentTypeName = [[YWEquipmentManager shareManager] equipmentTypeNameWithTypeId:equipmentTypeId];
    eleEquType.equipmentTypeId = equipmentTypeId;
    eleEquType.equipmentTypeName = equipmentTypeName;
    YWMeterWaitReadingTableViewController * waitReadingVC = [[YWMeterWaitReadingTableViewController alloc] init];
    waitReadingVC.equipmentType = eleEquType;
    [self.navigationController pushViewController:waitReadingVC animated:YES];
}

- (void)eleMonthLeftBtnEvent:(UIButton*)sender{
    YWEquipmentType * eleEquType = [[YWEquipmentType alloc] init];
    NSInteger equipmentTypeId = [[YWMeterReadingManager shareManager] electricMeterEquipmentTypeId];
    NSString *equipmentTypeName = [[YWEquipmentManager shareManager] equipmentTypeNameWithTypeId:equipmentTypeId];
    eleEquType.equipmentTypeId = equipmentTypeId;
    eleEquType.equipmentTypeName = equipmentTypeName;
    YWMeterWaitReadingTableViewController * waitReadingVC = [[YWMeterWaitReadingTableViewController alloc] init];
    waitReadingVC.equipmentType = eleEquType;
    [self.navigationController pushViewController:waitReadingVC animated:YES];
}

- (void)gasMonthLeftBtnEvent:(UIButton*)sender{
    YWEquipmentType * eleEquType = [[YWEquipmentType alloc] init];
    NSInteger equipmentTypeId = [[YWMeterReadingManager shareManager] gasMeterEquipmentTypeId];
    NSString *equipmentTypeName = [[YWEquipmentManager shareManager] equipmentTypeNameWithTypeId:equipmentTypeId];
    eleEquType.equipmentTypeId = equipmentTypeId;
    eleEquType.equipmentTypeName = equipmentTypeName;
    YWMeterWaitReadingTableViewController * waitReadingVC = [[YWMeterWaitReadingTableViewController alloc] init];
    waitReadingVC.equipmentType = eleEquType;
    [self.navigationController pushViewController:waitReadingVC animated:YES];
}

#pragma mark - UIKeyboardNSNotification

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //键盘弹起的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    //键盘隐藏的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (void)keyboardWillShow:(NSNotification*)notification
{
    NSDictionary* info = [notification userInfo];
    NSValue* value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
    CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    [self moveUpBgViewWithKeyboardHeight:keyboardSize.height animationDuration:duration];
}

- (void)keyboardWillHide:(NSNotification*)notification
{
    NSDictionary* info = [notification userInfo];
    CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    [self moveDownBgViewWithDuration:duration];
}

- (void)moveUpBgViewWithKeyboardHeight:(CGFloat)height animationDuration:(CGFloat)duration
{
    CGRect parentRect = [self.meterNumberTf.superview convertRect:self.meterNumberTf.frame toView:self.view];
    CGFloat textViewMaxY = CGRectGetMaxY(parentRect);
    if (self.view.height - textViewMaxY >= height) {
        return;
    }
    [UIView animateWithDuration:duration animations:^{
        self.view.y = -(height - self.view.height + textViewMaxY);
    }];
}

- (void)moveDownBgViewWithDuration:(CGFloat)duration
{
    if (64 == self.view.y) {
        return;
    }
    [UIView animateWithDuration:duration animations:^{
        self.view.y = 64;
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
